package com.xujl.mvcbind.ui.db.control;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.xujl.fastlib.base.BaseFragment;
import com.xujl.fastlib.base.BaseRvAdapter;
import com.xujl.mvcbind.R;
import com.xujl.mvcbind.adapter.OverTimeAdapter;
import com.xujl.mvcbind.bean.OverTimeEntity;
import com.xujl.mvcbind.consts.PermissionGroup;
import com.xujl.mvcbind.ui.db.model.OverTimeFragmentModel;
import com.xujl.mvcbind.ui.db.view.OverTimeFragmentView;
import com.xujl.mvcbind.util.RefreshLayoutHelper;
import com.xujl.mvcbind.window.MessageConfirmWindow;

import java.util.List;

import androidx.annotation.NonNull;
import io.reactivex.functions.Consumer;

/**
 * <pre>
 *     author : xujl
 *     e-mail : 597355068@qq.com
 *     time   : 2019/08/20
 *     desc   : 加班计时器
 *     version: 1.0
 * </pre>
 */

public class OverTimeFragmentControl extends BaseFragment<OverTimeFragmentModel, OverTimeFragmentView> implements OnItemChildClickListener, OnRefreshLoadMoreListener {
    private RefreshLayoutHelper<OverTimeEntity> mRefreshLayoutHelper;

    public static OverTimeFragmentControl newInstance () {
        Bundle args = new Bundle();
        OverTimeFragmentControl fragment = new OverTimeFragmentControl();
        fragment.setArguments(args);
        return fragment;
    }

    private BaseRvAdapter mAdapter;

    @Override
    public void init (Bundle savedInstanceState) {
        requestPermission();
    }

    private void init () {
        mModel.initDb();
        mAdapter = new OverTimeAdapter(mModel.getDataList());
        mView.setAdapter(mAdapter);
        mAdapter.addChildClickViewIds(R.id.btn_edit,R.id.tv_delete);
        mAdapter.setOnItemChildClickListener(this);
        mRefreshLayoutHelper = new RefreshLayoutHelper<>(mModel.getDataList(), mAdapter);
        mView.initRefresh(mRefreshLayoutHelper);
        mView.setPageData(mModel.getPageEntity());
        mRefreshLayoutHelper.startRefresh();
    }

    private void requestPermission () {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(PermissionGroup.SD_CARD)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept (Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            init();
                        } else {
                            mView.toast("没有权限操作");
                        }
                    }
                });
    }


    @Override
    public void onClick (View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.btn_add:
                exposeActivity().start(AddOverTimeFragmentControl.newInstance(null));
                break;
            default:

                break;

        }
    }

    @Override
    public void onItemChildClick (BaseQuickAdapter adapter, View view, final int position) {
        switch (view.getId()) {
            case R.id.btn_edit:
                exposeActivity().start(AddOverTimeFragmentControl.newInstance(mModel.getDataList().get(position)));
                break;
            case R.id.tv_delete:
                mView.showConfirmWindow(new MessageConfirmWindow.Callback() {
                    @Override
                    public void onConfirm () {
                        mModel.deleteEntity(position);
                        mRefreshLayoutHelper.startRefresh();
                    }
                });
                break;
            default:

                break;

        }

    }

    @Override
    public void onLoadMore (@NonNull RefreshLayout refreshLayout) {
        mRefreshLayoutHelper.loadMore();
        searchList();
    }

    @Override
    public void onRefresh (@NonNull RefreshLayout refreshLayout) {
        mModel.countTotal();
        mRefreshLayoutHelper.refresh();
        searchList();
    }

    private void searchList () {
        mModel.searchList(mRefreshLayoutHelper.getPage(), mRefreshLayoutHelper.getPageSize(), new OverTimeFragmentModel.OnDataResult() {
            @Override
            public void dataCallback (List<OverTimeEntity> list) {
                mRefreshLayoutHelper.saveResult(list);
            }
        });
    }
}
